package com.jecs.business;

import com.jecs.entity.KeyWord;
import org.wlld.naturalLanguage.word.MyKeyWord;
import com.jecs.bean.BeanMangerOnly;
import com.jecs.constant.ErrorCode;
import com.jecs.entity.Response;
import com.jecs.entity.Shop;
import com.jecs.tools.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wlld.naturalLanguage.languageCreator.CatchKeyWord;
import org.wlld.rnnJumpNerveCenter.RRNerveManager;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @param
 * @DATA
 * @Author LiDaPeng
 * @Description
 */
@Service
public class Business {
    @Autowired
    private BeanMangerOnly beanMangerOnly;

    public Response talk(String word) throws Exception {//对话返回分类
        Response response = new Response();
        if (word != null && word.replace(" ", "").length() > 1) {
            long eventId = SnowflakeIdWorker.get().nextId();//保证线程安全
            Shop shop = new Shop();//协议
            RRNerveManager rrNerveManager = beanMangerOnly.getRRNerveManager();//语言模型
            int type = rrNerveManager.getType(word, eventId);
            if (type > 0) {
                Map<Integer, MyKeyWord> myKeyWords = beanMangerOnly.getMyKeyWord();
                Map<Integer, CatchKeyWord> catchKeyWords = beanMangerOnly.catchKeyWord();
                if (myKeyWords.get(type).isKeyWord(word, eventId)) {//有关键词
                    Set<String> keyWords = catchKeyWords.get(type).getKeyWord(word);
                    shop.setKeywords(keyWords);
                }
            }
            shop.setType_id(type);
            response.setData(shop);
        } else {
            response.setError(ErrorCode.WordIsNull.getError());
            response.setErrorMessage(ErrorCode.WordIsNull.getErrorMessage());
        }
        return response;
    }

    private KeyWord keyWordEq(String word) {
        KeyWord myKeyWord = null;
        List<KeyWord> keyWordList = beanMangerOnly.getAllKeyWords().getKeyWords();
        for (KeyWord keyWord : keyWordList) {
            if (keyWord.getKeyword().equals(word)) {
                myKeyWord = keyWord;
                break;
            }
        }
        return myKeyWord;
    }

    private boolean isFree(String myWord) {//是否有折扣要求
        boolean isFree = false;
        List<String> freeWords = beanMangerOnly.getFreeWord();
        for (String word : freeWords) {
            if (myWord.contains(word)) {
                isFree = true;
                break;
            }
        }
        return isFree;
    }
}
